﻿package PinballCode {
	
	// Plunger class
	public class Plunger extends Rectangle{
		
		//Attrtibutes
		private var k:Number = 40; 			//spring constance
		private var startHeight:int;		// startHeight of the plunger
		private var compressRate:int = 50;	// rate at which the plunger compresses
			
		private var vy:Number = 0;			// velocity in the y
		private var friction:Number = .9;	// friction of plunger
		
		private var minHeight:Number;		// minimum allowed height
		
		
		// Constructor
		public function Plunger(ht:int,widt:int) {
			// constructor code
			super(-ht,widt);
			startHeight = this.ht;
			minHeight = startHeight/4;
		}
		
		// displacment of start and current height
		private function disp():Number {
			return startHeight - ht;
		}
		
		// when the space bar is held
		public function hold(dt:Number):void {
			ht += compressRate * dt;
			if(ht > minHeight)
				ht = minHeight;
			init();
		}
		
		// when the space bar is released
		public function release():Number {
			return disp() * k * .06 * friction;
		}
		
		
		// the plunger is trying to go back to its original state
		public function reset(dt:Number):void {
			this.vy = disp() * k * .04 * friction;
			ht += vy;
			init();
				
		}

	}
	
}
